package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.activities.messages.AddFeedInternal;
import com.wl156.activities.messages.ManageFeedInternal;
import com.wl156.lite.R;
import com.wl156.model.WebServiceClient;
import com.wl156.model.Wl156DB;
import com.wl156.utils.Utils;

public class MessageCenter extends BaseActivity implements OnClickListener {

	private static final String TAG = null;
	WebServiceClient mWebServiceClient;
	NotificationManager mNotificationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Window win = getWindow();
		// win.requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.act_message_center);
		// win.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
		// R.drawable.ic_launcher);

		mWebServiceClient = new WebServiceClient(this);
		String ns = Context.NOTIFICATION_SERVICE;
		mNotificationManager = (NotificationManager) getSystemService(ns);

		// findViewById(R.id.systemMessage).setOnClickListener(this);
		// findViewById(R.id.personalMessage).setOnClickListener(this);
		findViewById(R.id.scribeMessage).setOnClickListener(this);
		// findViewById(R.id.orderMessage).setOnClickListener(this);
		findViewById(R.id.addFeed).setOnClickListener(this);
		findViewById(R.id.manageFeed).setOnClickListener(this);
		findViewById(R.id.getInternalMessage).setOnClickListener(this);
	}
	
	@Override
	public void onClick(View view) {
		Class target = null;
		switch (view.getId()) {
		// case R.id.systemMessage:
		// break;
		// case R.id.personalMessage:
		// break;
		case R.id.scribeMessage:
			target = InternalMessage.class;
			break;
		// case R.id.orderMessage:
		// break;
		case R.id.addFeed:
			target = AddFeedInternal.class;
			break;
		case R.id.manageFeed:
			target = ManageFeedInternal.class;
			break;
		case R.id.getInternalMessage: {
			try {
				if (MyApp.sGlobleCertificate != null) {
					Object object = mWebServiceClient
							.requireNewInternalMessage(MyApp.sGlobleCertificate);
					if (object instanceof SoapPrimitive) {
						SoapPrimitive sp = (SoapPrimitive) object;
						if (Boolean.valueOf(sp.toString())) {
							Log.d(TAG, "有消息");
							String notificationText = "";
							Object response = mWebServiceClient
									.commonGetMyInternalMessage(MyApp.sGlobleCertificate);
							SoapObject soap;
							String content = "";
							StringBuilder sb = new StringBuilder();
							Wl156DB db = new Wl156DB(MessageCenter.this);
							String[] v = new String[7];

							if (response instanceof SoapObject) {
								soap = (SoapObject) response;
								for (int i = 0; i < soap.getPropertyCount(); i++) {
									SoapObject line = (SoapObject) soap
											.getProperty(i);
									String id = Utils.propertyToShow(line
											.getProperty(0));
									String type = Utils.propertyToShow(line
											.getProperty(1));
									String title = Utils.propertyToShow(line
											.getProperty(2));
									String carGoodType = Utils
											.propertyToShow(line.getProperty(3));
									String carOrGoodId = Utils
											.propertyToShow(line.getProperty(4));
									String date = Utils.propertyToShow(line
											.getProperty(5));
									try {
										content = Utils.propertyToShow(line
												.getProperty(6));
									} catch (Exception e) {
										content = "";
									}
									notificationText = title;
									v[0] = id;
									v[1] = type;
									v[2] = title;
									v[3] = date;
									v[4] = content;
									v[5] = carGoodType;
									v[6] = carOrGoodId;
									if (type.equals("特殊类型")) {
										String[] updateValues = new String[1];
										updateValues[0] = id;
										db.getWritableDatabase()
												.execSQL(
														"update internal_message set content='已成交' where id=?",
														updateValues);
									} else {
										db.getWritableDatabase()
												.execSQL(
														"insert into internal_message (id, type, title, date, content, carGoodType, carOrGoodId) "
																+ "values(?, ?, ?, ?, ?, ?, ?)",
														v);
									}
									sb.append(id).append("##").append(type)
											.append("##").append(title)
											.append("##").append(date)
											.append("##").append(content)
											.append("@@");

								}

								Intent i = new Intent();
								i.setAction("has_new_message");
								sendBroadcast(i);
							}
							db.close();

							CharSequence tickerText = "你有新的信息!";
							long when = System.currentTimeMillis();

							Notification notification = new Notification(
									R.drawable.icon, tickerText, when);
							Context context = getApplicationContext();
							CharSequence contentTitle = "你有新的信息";
							CharSequence contentText = notificationText;
							Intent notificationIntent = new Intent(
									MessageCenter.this, InternalMessage.class);
							notificationIntent.putExtra("msg", sb.toString());

							PendingIntent contentIntent = PendingIntent
									.getActivity(MessageCenter.this, 0,
											notificationIntent,
											PendingIntent.FLAG_UPDATE_CURRENT);

							notification.flags |= Notification.FLAG_AUTO_CANCEL;
							notification.defaults |= Notification.DEFAULT_ALL;
							notification.setLatestEventInfo(context,
									contentTitle, contentText, contentIntent);

							mNotificationManager.notify(0, notification);
						} else {
							Toast.makeText(MessageCenter.this, "现在没有消息",
									Toast.LENGTH_SHORT).show();
							Log.d(TAG, "没有消息");
						}
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

			break;
		}
		try {
			Intent i = new Intent(this, target);
			startActivity(i);
		} catch (Exception e) {
		}
	}
}
